package cn.gok.manage.service.impl;

import cn.gok.dao.UserMapper;
import cn.gok.manage.service.UserService;
import cn.gok.manage.web.vo.UserVO;
import cn.gok.pojo.User;
import cn.gok.util.JwtUtil;
import cn.gok.util.ResultData;
import cn.gok.util.UserCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 鸭鸭
 * @ClassName:UserServiceImpl
 * @Package:cn.gok.manage.service.impl
 * @Description:
 * @date: 2021/8/4
 * @time: 9:32
 */
@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public ResultData userLogin(String username, String password) {
        User u = userMapper.userLogin(username,password);
        if (u == null) {
            return ResultData.fail("用户名或密码错误");
        }
        UserVO userVO = new UserVO(u.getId(),u.getUsername(),u.getEmail(),u.getPhone(),u.getRole(),u.getCreate_time(),u.getUpdate_time());
        return ResultData.createData(userVO);
    }

    @Override
    public ResultData selectByUserName(String username) {

        String name = userMapper.selectByUserName(username);
        if(name != null){
            return ResultData.fail("用户已存在");
        }
        return ResultData.success();
    }

    @Override
    public ResultData userRegister(User user) {

        userMapper.userRegister(user);
        return ResultData.success("注册成功");
    }

    @Override
    public ResultData checkVaild(String str, String type) {
        Map map = new HashMap();
        map.put(type,str);
        String name = userMapper.checkVaild(map);
        if(name == null){
            return ResultData.success("校验成功");
        }
        return ResultData.fail("用户已存在");
    }

    @Override
    public ResultData selectAllByName(String username) {
        User u = userMapper.selectAllByName(username);
        if(u==null){
            return ResultData.fail("用户未登录,无法获取当前用户信息");
        }
        UserVO userVO = new UserVO(u.getId(),u.getUsername(),u.getEmail(),u.getPhone(),u.getRole(),u.getCreate_time(),u.getUpdate_time());
        return ResultData.createData(userVO);

    }

    @Override
    public ResultData forgetPwd(String username) {
        User u = userMapper.selectAllByName(username);
        if(u==null){
            return ResultData.fail("该用户不存在");
        }else{
            if(u.getQuestion()==null){
                return ResultData.fail("该用户未设置找回密码问题");
            }else{
                return ResultData.createData(u.getQuestion());
            }
        }
    }

    @Override
    public ResultData sendPwdAnswer(String username,String question,String answer) {
        User u = userMapper.selectAllByName(username);
        if(answer.equals(u.getAnswer()) && question.equals(u.getQuestion())){
            JwtUtil jwtUtil = new JwtUtil();
            String token = jwtUtil.CreateJwtTest(username);
            UserCacheManager.addCache(username,token);
            return ResultData.createData(token);
        }
        return ResultData.fail("问题答案错误");

    }

    @Override
    public ResultData updatePwdByAnswer(String password, String username) {
        if(UserCacheManager.getCache(username)!=null){
            int flag = userMapper.updatePasswordByAnswer(password,username);
            if(flag==1){
                UserCacheManager.delCache();
                return ResultData.fail("修改密码成功");

            }
            return ResultData.fail("密码修改失败");
        }
        return ResultData.fail("token已经失效");

    }

   @Override
    public ResultData updatePwdLogin(String passwordNew, String passwordOld, String username) {
        int flag = userMapper.updatePasswordLogin(passwordNew,passwordOld,username);
        if(flag==0){
            return ResultData.fail("旧密码输入错误");
        }
        return ResultData.success("修改密码成功");
    }

    @Override
    public ResultData updateInfoByName(User user) {
        Date date = new Date();
        user.setUpdate_time(date);
        int flag = userMapper.updateByPrimaryKeySelective(user);
        if(flag==0){
            return ResultData.fail("用户未登录");
        }
        return ResultData.success("更新个人信息成功");
    }

    @Override
    public ResultData getAllInfoByName(String username) {
        User user = userMapper.selectAllByName(username);
        if(user==null){

            return ResultData.fail_login("用户未登录,无法获取当前用户信息,status=10,强制登录");
        }
        return ResultData.createData(user);
    }

    @Override
    public ResultData queryAllUser(int pageNum, int pageSize) {
        return null;
    }
}